package com.youku
{
   import flash.display.MovieClip;
   import com.youku.skin.AssetManager;
   import com.youku.skin.YaheiFontManager;
   import com.youku.events.PlayerControlEvent;
   import com.youku.data.ApiData;
   import com.youku.events.ViewManagerEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.interfaces.IRootData;
   import com.youku.data.LoadTimeData;
   import com.youku.constants.RootConstant;
   import flash.display.StageScaleMode;
   import flash.display.StageAlign;
   import flash.ui.ContextMenuItem;
   import flash.ui.ContextMenu;
   import flash.display.DisplayObject;
   import com.youku.interfaces.IPartnerData;
   import com.youku.interfaces.IApiData;
   import com.youku.data.PartnerNobandData;
   import com.youku.data.PartenerSkinData;
   import com.youku.data.PartnerData;
   import com.youku.interfaces.IExPluginProxy;
   import com.youku.conductor.ConductorState;
   import com.youku.events.PlayerOutEvent;
   import flash.system.Security;
   
   public class PlayerShell extends MovieClip
   {
      
      public function PlayerShell() {
         super();
         Security.allowDomain("*");
         if(stage)
         {
            this.init();
            return;
         }
      }
      
      protected var _facadeManager:FacadeManager;
      
      protected var _viewManager:ViewManager;
      
      protected var _userActionDispatcher:UserActionDispatcher;
      
      protected var _playerProxy:PlayerProxy;
      
      protected var _exPluginProxy:ExPluginProxy;
      
      protected var _assetManager:AssetManager;
      
      protected function init(param1:Object = null) : void {
         this.setStageVariable();
         this.setContextMenu();
         this.setRootData(param1);
         this.setLoadTimeData(param1);
         this.setPlayerConfigVariable();
         this._assetManager = new AssetManager();
         YaheiFontManager.setYaheiFont();
         this._playerProxy = new PlayerProxy();
         GlobalService.addService(GlobalServiceKeys.PLAYER_PROXY,this._playerProxy);
         this._playerProxy.addEventListener(PlayerControlEvent.CM_MENU_UPDATED,this.onContextMenuUpdated);
         if(PlayerConfig.partnerData == null || PlayerConfig.partnerData.status == 0)
         {
            PlayerConfig.partnerData = null;
            PlayerConfig.rootData.partnerId = null;
         }
         if(PlayerConfig.apiData == null)
         {
            PlayerConfig.apiData = new ApiData();
         }
         this._viewManager = new ViewManager();
         GlobalService.addService(GlobalServiceKeys.VIEW_MANAGER,this._viewManager);
         addChild(this._viewManager);
         this._viewManager.addEventListener(ViewManagerEvent.FULLSCREEN_MODE_CHANGED,this.onViewManagerEvent);
         this._userActionDispatcher = new UserActionDispatcher();
         addChild(this._userActionDispatcher);
         GlobalService.addService(GlobalServiceKeys.USER_ACTION_DISPATCHER,this._userActionDispatcher);
         this._exPluginProxy = new ExPluginProxy();
         GlobalService.addService(GlobalServiceKeys.EXPLUGIN_PROXY,this._exPluginProxy);
         this._facadeManager = new FacadeManager();
         GlobalService.addService(GlobalServiceKeys.FACADE_MANAGER,this._facadeManager);
         this._facadeManager.init();
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
      }
      
      protected function setRootData(param1:Object) : void {
         var _loc2_:Object = param1;
         if(param1 == null)
         {
            _loc2_ = stage.loaderInfo.parameters;
         }
         var _loc3_:IRootData = RootDataParser.parseData(_loc2_);
         var _loc4_:String = this.loaderInfo.url;
         _loc4_ = _loc4_.substr(0,_loc4_.search(new RegExp("\\/[^\\/]*\\.swf")));
         _loc3_.baseURL = _loc4_;
         PlayerConfig.rootData = _loc3_;
      }
      
      protected function setLoadTimeData(param1:Object) : void {
         var _loc2_:Object = param1;
         if(param1 == null)
         {
            _loc2_ = stage.loaderInfo.parameters;
         }
         var _loc3_:LoadTimeData = new LoadTimeData();
         if(_loc2_["ltplayer"])
         {
            _loc3_.ltplayer = _loc2_["ltplayer"];
         }
         if(_loc2_["ltplaypolicy"])
         {
            _loc3_.ltplaypolicy = _loc2_["ltplaypolicy"];
         }
         PlayerConfig.loadtimeData = _loc3_;
      }
      
      protected function setPlayerConfigVariable() : void {
         PlayerConfig.isTouchPlayer = false;
         PlayerConfig.controlBarHeight = 43;
         if(PlayerConfig.rootData.autoHide == RootConstant.CONTROLBAR_MODE_HIDE || PlayerConfig.rootData.autoHide == RootConstant.CONTROLBAR_MODE_NONE)
         {
            PlayerConfig.controlBarHeight = 0;
         }
         else if(PlayerConfig.rootData.autoHide == RootConstant.CONTROLBAR_MODE_HALF)
         {
            PlayerConfig.controlBarHeight = 13;
         }
         
      }
      
      protected function setStageVariable() : void {
         stage.scaleMode = StageScaleMode.NO_SCALE;
         stage.align = StageAlign.TOP_LEFT;
         stage.tabChildren = false;
         stage.showDefaultContextMenu = false;
      }
      
      protected function setContextMenu() : void {
         var _loc1_:ContextMenuItem = new ContextMenuItem("version: " + "4.1." + VersionInfo.ver());
         var _loc2_:ContextMenu = new ContextMenu();
         _loc2_.hideBuiltInItems();
         _loc2_.customItems.push(_loc1_);
         this.contextMenu = _loc2_;
      }
      
      protected function onContextMenuUpdated(param1:PlayerControlEvent) : void {
         if(param1.data.menu == null || param1.data.menu as ContextMenu == null)
         {
            return;
         }
         this.contextMenu = param1.data.menu;
      }
      
      public function initPlayer(param1:Object, param2:Object = null, param3:DisplayObject = null) : void {
         var _loc4_:IPartnerData = this.parsePartnerData(param1);
         var _loc5_:IApiData = this.parseApiData(param1);
         PlayerConfig.partnerData = _loc4_;
         PlayerConfig.apiData = _loc5_;
         PlayerConfig.loadingMC = param3;
         this.init(param2);
      }
      
      protected function parsePartnerData(param1:Object) : IPartnerData {
         var _loc3_:PartnerNobandData = null;
         var _loc4_:PartenerSkinData = null;
         if(param1 == null)
         {
            return null;
         }
         var _loc2_:IPartnerData = new PartnerData();
         if(param1.hasOwnProperty("status"))
         {
            _loc2_.status = param1.status;
         }
         if(param1.hasOwnProperty("passless"))
         {
            _loc2_.passless = param1.passless;
         }
         if(param1.hasOwnProperty("atm"))
         {
            _loc2_.atm = param1.atm;
         }
         if(param1.hasOwnProperty("token"))
         {
            _loc2_.token = param1.token;
         }
         if(param1.hasOwnProperty("noband"))
         {
            _loc3_ = new PartnerNobandData();
            if(param1.noband.hasOwnProperty("status"))
            {
               _loc3_.status = param1.noband.status;
            }
            if(param1.noband.hasOwnProperty("loadingurl"))
            {
               _loc3_.loadingurl = param1.noband.loadingurl;
            }
            _loc2_.nobandData = _loc3_;
         }
         if(param1.style)
         {
            _loc4_ = new PartenerSkinData();
            if(param1.style.status)
            {
               _loc4_.status = param1.style.status;
            }
            if(param1.style.controllbar)
            {
               if(param1.style.controllbar.bg)
               {
                  _loc4_.controllbar_bg = param1.style.controllbar.bg;
               }
               if(param1.style.controllbar.btn)
               {
                  _loc4_.controllbar_btn = param1.style.controllbar.btn;
               }
               _loc4_.isControllbarDiv = true;
            }
            if(param1.style.progressbar)
            {
               if(param1.style.progressbar.cached)
               {
                  _loc4_.progressbar_cached = param1.style.progressbar.cached;
               }
               if(param1.style.progressbar.played)
               {
                  _loc4_.progressbar_played = param1.style.progressbar.played;
               }
               if(param1.style.progressbar.cacheless)
               {
                  _loc4_.progressbar_cacheless = param1.style.progressbar.cacheless;
               }
               if(param1.style.progressbar.dot)
               {
                  _loc4_.progressbar_dot = param1.style.progressbar.dot;
               }
               _loc4_.isProgressbarDiv = true;
            }
            if(param1.style.setup)
            {
               if(param1.style.setup.btn)
               {
                  _loc4_.setup_btn = param1.style.setup.btn;
               }
               if(param1.style.setup.txt)
               {
                  _loc4_.setup_txt = param1.style.setup.txt;
               }
               _loc4_.isSetupDiv = true;
            }
            _loc2_.skinData = _loc4_;
         }
         return _loc2_;
      }
      
      protected function parseApiData(param1:Object) : IApiData {
         if(param1 == null || param1.api == null)
         {
            return null;
         }
         var _loc2_:IApiData = new ApiData();
         if(param1.api.nsseek)
         {
            _loc2_.nsseek = param1.api.nsseek;
         }
         if(param1.api.setContinuous)
         {
            _loc2_.setContinuous = param1.api.setContinuous;
         }
         if(param1.api.setSkinColor)
         {
            _loc2_.setSkinColor = param1.api.setSkinColor;
         }
         if(param1.api.pauseVideo)
         {
            _loc2_.pauseVideo = param1.api.pauseVideo;
         }
         if(param1.api.playVideoByID)
         {
            _loc2_.playVideoByID = param1.api.playVideoByID;
         }
         if(param1.api.showControlBar)
         {
            _loc2_.showControlBar = param1.api.showControlBar;
         }
         return _loc2_;
      }
      
      protected function onViewManagerEvent(param1:ViewManagerEvent) : void {
         var _loc2_:* = false;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         switch(param1.type)
         {
            case ViewManagerEvent.FULLSCREEN_MODE_CHANGED:
               _loc2_ = param1.data as Boolean;
               if(_loc2_)
               {
                  _loc3_ = stage.fullScreenWidth;
                  _loc4_ = stage.fullScreenHeight;
                  this._viewManager.x = 0;
                  this._viewManager.y = 0;
               }
               else
               {
                  this._viewManager.x = 0;
                  this._viewManager.y = 0;
               }
               break;
         }
      }
      
      protected function get externalProxy() : ExternalProxy {
         return GlobalService.getService(GlobalServiceKeys.EXTERNALINTERFACE_PROXY) as ExternalProxy;
      }
      
      public function getNsData() : Object {
         var _loc1_:ExternalProxy = GlobalService.getService(GlobalServiceKeys.EXTERNALINTERFACE_PROXY) as ExternalProxy;
         return _loc1_.getNsData();
      }
      
      public function getSize() : Object {
         return this.externalProxy.getSize();
      }
      
      public function nsseek(param1:int) : void {
         this.externalProxy.nsseek(param1);
      }
      
      public function pauseVideo(param1:Boolean, param2:Boolean = true) : void {
         this.externalProxy.pauseVideo(param1,param2);
      }
      
      public function playVideoByID(param1:String, param2:String = "") : void {
         this.externalProxy.playVideoByID(param1,param2);
      }
      
      public function stopVideo() : void {
         this.externalProxy.stopVideo();
      }
      
      public function isPause() : Boolean {
         return this.externalProxy.isPause();
      }
      
      public function showControlBar(param1:Boolean) : void {
         this.externalProxy.showControlBar(param1);
      }
      
      public function setSize(param1:Number, param2:Number) : void {
         if(this._viewManager)
         {
            this._viewManager.setSize(param1,param2);
         }
      }
      
      public function get exProxy() : IExPluginProxy {
         return this._exPluginProxy;
      }
      
      public function setVolume(param1:Number) : void {
         this.externalProxy.setVolume(param1);
      }
      
      public function getVolume() : Number {
         return this.externalProxy.getVolume();
      }
      
      public function setLoop(param1:Boolean) : void {
         this.externalProxy.setLoop(param1);
      }
      
      public function getPlaybackQuality() : String {
         return this.externalProxy.getPlaybackQuality();
      }
      
      public function setPlaybackQuality(param1:String) : void {
         this.externalProxy.setPlaybackQuality(param1);
      }
      
      public function getAvailableQualityLevels() : Array {
         return this.externalProxy.getAvailableQualityLevels();
      }
      
      public function getJump() : Boolean {
         return this.externalProxy.getJump();
      }
      
      public function setJump(param1:Boolean) : void {
         this.externalProxy.setJump(param1);
      }
      
      public function getThreed() : Object {
         return this.externalProxy.getThreed();
      }
      
      public function setShowMode(param1:String) : void {
         this.externalProxy.setShowMode(param1);
      }
      
      public function setAdjust(param1:Number = 0, param2:Number = 0, param3:Number = 0) : void {
         this.externalProxy.setAdjust(param1,param2,param3);
      }
      
      public function hasWatermark() : Boolean {
         return this.externalProxy.hasWatermark();
      }
      
      public function setLanguage(param1:String) : void {
         this.externalProxy.setLanguage(param1);
      }
      
      public function getLanguage() : String {
         return this.externalProxy.getLanguage();
      }
      
      protected function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         if(_loc2_ == ConductorState.VIDEO_END)
         {
            dispatchEvent(new PlayerOutEvent(PlayerOutEvent.VIDEO_STOP));
         }
      }
      
      public function getPlayerState() : String {
         return this.externalProxy.getPlayerState();
      }
      
      public function getVersion() : String {
         return VersionInfo.ver() + "";
      }
   }
}
